package org.ehe.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.system.domain.bo.HrEmployeeBo;
import org.ehe.system.domain.vo.HrEmployeeVo;
import org.ehe.system.service.IHrEmployeeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 员工主信息管理
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/employee")
public class HrEmployeeController extends BaseController {

    private final IHrEmployeeService employeeService;

    /**
     * 查询员工主信息列表
     */
    @SaCheckPermission("system:employee:list")
    @GetMapping("/list")
    public TableDataInfo<HrEmployeeVo> list(HrEmployeeBo bo, PageQuery pageQuery) {
        return employeeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出员工主信息列表
     */
    @SaCheckPermission("system:employee:export")
    @Log(title = "员工主信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HrEmployeeBo bo) {
        List<HrEmployeeVo> list = employeeService.queryList(bo);
        // 这里可以使用ExcelUtil导出，暂时简化
    }

    /**
     * 获取员工主信息详细信息
     *
     * @param employeeId 员工主键
     */
    @SaCheckPermission("system:employee:query")
    @GetMapping("/{employeeId}")
    public R<HrEmployeeVo> getInfo(@PathVariable Long employeeId) {
        return R.ok(employeeService.queryById(employeeId));
    }

    /**
     * 根据工号获取员工信息
     *
     * @param employeeNo 工号
     */
    @SaCheckPermission("system:employee:query")
    @GetMapping("/no/{employeeNo}")
    public R<HrEmployeeVo> getInfoByNo(@PathVariable String employeeNo) {
        return R.ok(employeeService.queryByEmployeeNo(employeeNo));
    }

    /**
     * 新增员工主信息
     */
    @SaCheckPermission("system:employee:add")
    @Log(title = "员工主信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HrEmployeeBo bo) {
        if (!employeeService.checkEmployeeNoUnique(bo.getEmployeeNo(), null)) {
            return R.fail("新增员工失败，工号已存在");
        }
        return toAjax(employeeService.insertByBo(bo));
    }

    /**
     * 修改员工主信息
     */
    @SaCheckPermission("system:employee:edit")
    @Log(title = "员工主信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HrEmployeeBo bo) {
        if (!employeeService.checkEmployeeNoUnique(bo.getEmployeeNo(), bo.getEmployeeId())) {
            return R.fail("修改员工失败，工号已存在");
        }
        return toAjax(employeeService.updateByBo(bo));
    }

    /**
     * 删除员工主信息
     *
     * @param employeeIds 员工主键串
     */
    @SaCheckPermission("system:employee:remove")
    @Log(title = "员工主信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{employeeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] employeeIds) {
        return toAjax(employeeService.deleteByIds(Arrays.asList(employeeIds)));
    }

    /**
     * 员工离职
     *
     * @param employeeId  员工ID
     * @param leaveDate   离职日期
     * @param leaveReason 离职原因
     */
    @SaCheckPermission("system:employee:edit")
    @Log(title = "员工离职", businessType = BusinessType.UPDATE)
    @PutMapping("/leave")
    public R<Void> employeeLeave(@RequestParam @NotNull(message = "员工ID不能为空") Long employeeId,
            @RequestParam String leaveDate,
            @RequestParam(required = false) String leaveReason) {
        return toAjax(employeeService.employeeLeave(employeeId, leaveDate, leaveReason));
    }

    /**
     * 自动生成员工工号
     */
    @GetMapping("/generateEmployeeCode")
    public R<String> generateEmployeeCode() {
        String s = employeeService.generateEmployeeCode();
        return R.ok("新工号",s);
    }
}
